package com.qianguo.shop.assistant;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.shop.R;
import com.qianguo.shop.base.BaseTitleActivity;
import com.qianguo.shop.constant.ShopKeys;
import com.qianguo.shop.entity.KeyValuePair;
import com.qianguo.shop.entity.ShopPosition;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 店员职位列表
 *
 * @author eva
 * @date 16/3/11
 */
@EActivity(R.layout.activity_shop_position_list)
public class ShopPositionListActivity extends BaseTitleActivity {

    @ViewById(R.id.shop_position_list_view)
    ListView mListView;

    @Extra(ShopKeys.EXTRA_KEY_SHOP_ID)
    String mShopID;

    private ShopPosition mShopPosition;

    private PositionListAdapter mAdapter;

    public static void jumpToPositions(Activity context, int requestCode, String shopID) {
        Intent intent = new Intent(context, com.qianguo.shop.assistant.ShopPositionListActivity_.class);
        intent.putExtra(ShopKeys.EXTRA_KEY_SHOP_ID, shopID);
        context.startActivityForResult(intent, requestCode);
    }

    protected String getToolbarTitle() {
        return getString(R.string.position);
    }

    @Override
    protected void initializeUI() {
        mAdapter = new PositionListAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(ShopKeys.RESULT_KEY_POSITION, (KeyValuePair) mAdapter.getItem(position));
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        getPositions();
    }

    @Background
    void getPositions() {
        mApp.getApi().getRoles(new HttpRequestCallback<ShopPosition>() {
            @Override
            public void onSuccess(int code, String msg, ShopPosition data) {
                mShopPosition = data;
                refreshPositions();
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    @UiThread
    void refreshPositions() {
        mAdapter.notifyDataSetChanged();
    }

    public class PositionListAdapter extends BaseAdapter {

        public PositionListAdapter(Context context) {

        }

        @Override
        public int getCount() {
            return mShopPosition != null ? mShopPosition.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mShopPosition.getItem(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(ShopPositionListActivity.this).inflate(R.layout.item_shop_roles, null);
            TextView role = (TextView) convertView.findViewById(R.id.item_shop_role_tv);
            role.setText(mShopPosition.getValue(position));
            return convertView;
        }
    }
}
